<template>
	<div class="system-dic-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="dicDialogFormRef" :model="state.ruleForm" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="登录账号"  prop="account">
							<el-input v-model="state.ruleForm.account" placeholder="请输入登录账号" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="登录密码"  prop="password">
							<el-input v-model="state.ruleForm.password" placeholder="请输入登录密码" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="企业">
							<el-select v-model="state.ruleForm.company_id" placeholder="请选择" clearable class="w100">
								<el-option v-for="item in state.companyData" 
								:key="item.id"  :label="item.name"  :value="item.id" ></el-option>
							</el-select>
						</el-form-item>
					</el-col> 
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="状态">
							<el-switch v-model="state.ruleForm.status" inline-prompt active-text="启" inactive-text="禁" :active-value="1" :inactive-value="2"></el-switch>
						</el-form-item>
					</el-col> 
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="备注">
							<el-input v-model="state.ruleForm.remark" placeholder="请输入备注"  clearable></el-input>
						</el-form-item>
					</el-col> 
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="companyDialog">
import { reactive, ref,nextTick } from 'vue';

import { getAPI } from '/@/utils/axios-utils';
import { CompanyUserApi,CompanyApi } from '/@/api-services/api';
import { CompanyAccountOut,CompanyOut } from '/@/api-services/models';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const dicDialogFormRef = ref();
const state = reactive({
	ruleForm: {} as CompanyAccountOut, 
	companyData:[] as CompanyOut[],
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

// 打开弹窗
const openDialog = (type: string, row: CompanyAccountOut) => {
	if (type === 'edit') {
		state.ruleForm = row;
		state.dialog.title = '修改企业用户';
		state.dialog.submitTxt = '修 改';
	} else {
		state.dialog.title = '新增企业用户';
		state.dialog.submitTxt = '新 增';
		
		state.ruleForm={
			id:'',
			company_id:'',
			account:'',
			password:'',
			remark:'',
			status:1
		}
		
	}
	state.dialog.isShowDialog = true;
	state.dialog.type=type;
	getCompanyData()
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = () => {
	dicDialogFormRef.value.validate(async (valid: boolean) => {
		if (!valid) return;
 
		if (state.dialog.type=="edit") {
			await getAPI(CompanyUserApi).companyaccountUpdate(state.ruleForm);
		} else {
			await getAPI(CompanyUserApi).companyaccountAdd(state.ruleForm);
		}
		closeDialog();
		emit('refresh'); 
	});
}; 

// 初始化角色数据
const getCompanyData = async() => {
	var res = await getAPI(CompanyApi).companyList({});

	state.companyData =  res.data.data ?? [];
};


// 暴露变量
defineExpose({
	openDialog,
});
</script>
